﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using Benefit.Models.Sys;
namespace Benefit.Models.Month
{
    /// <summary>
    /// 账户月预期值
    /// </summary>
    public class AccountMonthExpected
    {
        public int Id { get; set; }

        public int AccountId { get; set; }

        public virtual Sys.Account Account { get; set; }

        public int Year { get; set; }

        public int Month { get; set; }

        /// <summary>
        /// 平仓盈亏
        /// </summary>
        public double Profit { get; set; }
        /// <summary>
        /// 手续费
        /// </summary>
        public double Free { get; set; }
        /// <summary>
        /// 净收益
        /// </summary>
        public double DayCount { get; set; }
        /// <summary>
        /// 交易笔数
        /// </summary>
        public double BillCount { get; set; }
        /// <summary>
        /// 总亏损
        /// </summary>
        public double SumLost { get; set; }
        /// <summary>
        /// 总盈利
        /// </summary>
        public double SumWin { get; set; }
        /// <summary>
        /// 保证金占用
        /// </summary>
        public double UsedMargin { get; set; }
        /// <summary>
        /// 盈利次数
        /// </summary>
        public int WinCount { get; set; }
        /// <summary>
        /// 亏损次数
        /// </summary>
        public int LoseCount { get; set; }


        //public void Delete(int month, int year, DB.DBManager db)
        //{
        //    var query = from t in db.AccountMonthExpected where t.Year == year && t.Month == month select t;
        //    foreach (Data.Month.AccountMonthExpected c in query)
        //    {
        //        db.AccountMonthExpected.Remove(c);
        //    }
        //    db.SaveChanges();
        //}

        ///// <summary>
        ///// 初始化账户月预期值
        ///// </summary>
        ///// <param name="week"></param>
        ///// <param name="month"></param>
        ///// <param name="year"></param>
        //public void InitAccountMonthExpected(int month, int year, DB.DBManager db)
        //{
        //    var query = db.Accounts.ToList();
        //    foreach (Sys.Account account in query)
        //    {
        //        Data.Month.AccountMonthExpected change = new Data.Month.AccountMonthExpected();
        //        change.AccountId = account.Id;
        //        change.Month = month;
        //        change.Year = year;
        //        var existQuery = from t in db.AccountMonthExpected where t.AccountId == account.Id && t.Month == month && t.Year == year select t;
        //        if (existQuery.Count() > 0)
        //        {
        //            change = existQuery.First();
        //        }
        //        var _query = from t in db.AccountDayAnalysis where t.AccountId == account.Id && t.TradeHistory.MPdate == month && t.TradeHistory.YPdate == year orderby t.Id descending select t;
        //        double raiserate = 0;
        //        if (_query.Count() > 0)
        //        {
        //            raiserate = Math.Round(_query.Average(a => a.RaiseRate), 2);
        //        }
        //        var _query2 = from t in db.AccountDayChange where t.AccountId == account.Id && t.TradeHistory.MPdate == month && t.TradeHistory.YPdate == year orderby t.Id descending select t;
        //        double avgbenefit = 0;
        //        if (_query2.Count() > 0)
        //        {
        //            avgbenefit = Math.Round(_query2.Average(a => a.DayCount), 2);
        //        }
        //        change.DayCount = avgbenefit * 20 * raiserate;

        //        if (existQuery.Count() == 0)//没有本月数据
        //        {
        //            db.AccountMonthExpected.Add(change);
        //        }
        //    }
        //    db.SaveChanges();
        //}

    }
}